﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EazyCMS.Common;

namespace EazyCMS.Web.admin.article
{
    public partial class student_edit : Web.UI.ManagePage
    {
        string defaultpassword = "0|0|0|0"; //默认显示密码
        protected string action = EazyEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = EazyRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == EazyEnums.ActionEnum.Edit.ToString())
            {
                this.action = EazyEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = EazyRequest.GetQueryInt("id");
               
            }
            if (!Page.IsPostBack)
            {
              //  ChkAdminLevel("user_list", EazyEnums.ActionEnum.View.ToString()); //检查权限
              //  TreeBind("is_lock=0"); //绑定类别
                if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        //#region 绑定类别=================================
        //private void TreeBind(string strWhere)
        //{
        //    BLL.user_groups bll = new BLL.user_groups();
        //    DataTable dt = bll.GetList(0, strWhere, "grade asc,id asc").Tables[0];

        //    this.ddlGroupId.Items.Clear();
        //    this.ddlGroupId.Items.Add(new ListItem("请选择组别...", ""));
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        this.ddlGroupId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
        //    }
        //}
        //#endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.jyj_student bll = new BLL.jyj_student();
            Model.student_info model = bll.GetModel(_id);

            //ddlGroupId.SelectedValue = model.group_id.ToString();
            //rblStatus.SelectedValue = model.status.ToString();

            txtUserCard.Text = model.User_card;
            txtUserName.Text=model.User_name;
            txtUserClass.Text = model.User_class;
            txtUserSchool.Text = model.User_school;
            txtUserSex.Text = model.User_sex;
            txtUserTicket.Text = model.User_ticket;
            txtUserYear.Text = model.User_year;

           
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.student_info model = new Model.student_info();
            BLL.jyj_student bll = new BLL.jyj_student();
            model.User_card = txtUserCard.Text.Trim();
            model.User_class = txtUserClass.Text.Trim();
            model.User_name = txtUserName.Text.Trim();
            model.User_school = txtUserSchool.Text.Trim();
            model.User_sex = txtUserSex.Text.Trim();
            model.User_ticket = txtUserTicket.Text.Trim();
            model.User_year = txtUserYear.Text.Trim();
            if (bll.Add(model) > 0)
            {            
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.jyj_student bll = new BLL.jyj_student();
            Model.student_info model = bll.GetModel(_id);

            model.User_card = txtUserCard.Text.Trim();
            model.User_class = txtUserClass.Text.Trim();
            model.User_name = txtUserName.Text.Trim();
            model.User_school = txtUserSchool.Text.Trim();
            model.User_sex = txtUserSex.Text.Trim();
            model.User_ticket = txtUserTicket.Text.Trim();
            model.User_year = txtUserYear.Text.Trim();
         
            if (bll.Update(model))
            {
                //AddAdminLog(EazyEnums.ActionEnum.Edit.ToString(), "修改用户信息:" + model.user_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
            {
                //ChkAdminLevel("student_list.aspx", EazyEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改学生信息成功！", "student_list.aspx");
            }
            else //添加
            {
                //ChkAdminLevel("user_list", EazyEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加学生信息成功！", "student_list.aspx");
            }
        }

    }
}